﻿using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.MusicStore.Models;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Input;

namespace Avalonia.MusicStore.ViewModels;

public class MainViewModel : ViewModelBase
{  

    public ICommand BuyMusicCommand { get; }

    public Interaction<MusicStoreViewModel, AlbumViewModel?> ShowDialog { get; }

    public ObservableCollection<AlbumViewModel> Albums { get; } = [];

    private ObservableCollection<Person> _people;

    public FlatTreeDataGridSource<Person> PersonSource { get; }

    public MainViewModel()
    {
        _people = new ObservableCollection<Person>()
            {
                new Person ("Eleanor", "Pope", 32 ),
                new Person ("Jeremy", "Navarro", 74 ),
                new Person ( "Lailah ", "Velazquez", 16 ),
                new Person ( "Jazmine", "Schroeder", 52 ),
            };

        PersonSource = new FlatTreeDataGridSource<Person>(_people)
        {
            Columns =
                {
                    new TextColumn<Person, string>
                        ("First Name", x => x.FirstName),
                    new TextColumn<Person, string>
                        ("Last Name", x => x.LastName),
                    new TextColumn<Person, int>
                        ("Age", x => x.Age),
                },
        };

        //var all = Album.SearchAsync("i").Result;
        //foreach (var item in all)
        //{
        //    var vm = new AlbumViewModel(item);
        //    Albums.Add(vm);
        //}
        //RxApp.MainThreadScheduler.Schedule(LoadAlbums);

        //string s = """
        //    [{"Artist":"ILO","Title":"Gather People Together","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music125/v4/c6/38/75/c63875a8-38ee-dc14-0579-4a71dc3f77db/mzi.wnhmijts.jpg/600x600bb.jpg"},{"Artist":"ILO","Title":"ILO - EP","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music/d9/f2/78/mzi.nnanlmwk.jpg/600x600bb.jpg"},{"Artist":"Skee-Lo","Title":"I Wish","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music124/v4/ce/06/a7/ce06a793-cf8f-4b39-d2d8-900d4dda4404/mzi.hrrxiuqq.jpg/600x600bb.jpg"},{"Artist":"Jennifer Lopez","Title":"A Selena Tribute: Como La Flor / Bidi Bidi Bom Bom / Amor Prohibido / I Could Fall In Love / No Me Queda Mas - Single","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music7/v4/c2/96/2e/c2962e08-10cb-92dc-cef8-4759cdab0ef4/886445266283.jpg/600x600bb.jpg"},{"Artist":"CeeLo Green \u0026 Timbaland","Title":"I\u0027ll Be Around (feat. Timbaland) [Radio Mix] - Single","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music/bd/19/c8/mzi.yuvjahfv.jpg/600x600bb.jpg"},{"Artist":"ILO","Title":"Mind Like a Maze (unplugged)","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music122/v4/96/c7/13/96c7132d-6d23-94ac-8d98-35fcc7ef1a78/5690738102708_Cover.jpg/600x600bb.jpg"},{"Artist":"RHYME BOYA, i.LO \u0026 ITTOK","Title":"EMERALD HEAT (feat. \u7940SP) - Single","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/72/d6/4d/72d64d02-5c5f-332e-b664-bff08910886f/859755839140_cover.jpg/600x600bb.jpg"},{"Artist":"ILO","Title":"Homes - EP","CoverUrl":"https://is1-ssl.mzstatic.com/image/thumb/Music124/v4/71/05/02/710502a6-a434-5a89-d02b-8739e99868ba/cover.jpg/600x600bb.jpg"}]
        //    """;
        //var resList = JsonSerializer.Deserialize<List<Album>>(s);
        //foreach (var item in resList)
        //{
        //    var vm = new AlbumViewModel(item);
        //    Albums.Add(vm);
        //}

        ShowDialog = new Interaction<MusicStoreViewModel, AlbumViewModel?>();

        BuyMusicCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            var store = new MusicStoreViewModel();

            var result = await ShowDialog.Handle(store);
            if (result != null)
            {
                Albums.Add(result);
                await result.SaveToDiskAsync();
            }

        });

      

    }

    private async void LoadAlbums()
    {
        var albums = (await Album.LoadCachedAsync()).Select(x => new AlbumViewModel(x));

        foreach (var album in albums)
        {
            Albums.Add(album);
        }

        foreach (var album in Albums.ToList())
        {
            await album.LoadCover();
        }
    }

}
